package cho114366.basic;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class J23_MemberDAO implements J23_MemberService {

	private static final String DRV = "oracle.jdbc.OracleDriver";
	private static final String URL = "jdbc:oracle:thin:@//localhost:1521/xe";
	private static final String USR = "scott";
	private static final String PWD = "TIGER";

	// SQL문
	// 정의--------------------------------------------------------------------------------------------

	private static final String insertSQL = " insert into member(userid,jumin,pwd,zip,addr,tel,mp,pay) VALUES('abc','123456-123456','123456','','','','123-456-7-987','무통장입금') ";
	// 오라클 확인// 그대로//가져와라
	private static final String selectSQL = " SELECT * from member where userid=? ";
	private static final String select1SQL = " select userid,jumin,mp from member order by userid ";
	private static final String updateSQL = " update member set pwd='987', zip='987-123',addr='서울 광진구 자양동',tel='123-456-234',mp='987-986-3434',pay='신용카드'where userid='abc' ";
	private static final String deleteSQL = " delete from member where userid='abc' ";

	@Override
	public int insertMember(J23Member m) {
		int cnt = 0;
		return cnt;
	}

	@Override
	public int updateMember(J23Member m) {
		int cnt = 0;
		return cnt;
	}

	@Override
	public int deleteMember(String m) {
		int cnt = 0;
		return cnt;
	}

	@Override
	public ArrayList<J23Member> readMember() {
		ArrayList<J23Member> data = new ArrayList<>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {

			Class.forName(DRV);
			conn = DriverManager.getConnection(URL, USR, PWD);
			ps = conn.prepareStatement(select1SQL);
			rs = ps.executeQuery();
			while (rs.next()) {
				J23Member m = new J23Member(rs.getString(1), rs.getString(2),
						null, null, null, null, rs.getString(3), null);
				// 결과집합 rs 에 저장된 모든 데이터 중
				// 레코드 단위(행)로 하나씩 읽어와서
				// J23_Member 클래스에 저장하고
				// ArrayList data변수에 추가됨
				data.add(m);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (rs != null)
				try {
					rs.close();
				} catch (Exception e) {
				}
			if (ps != null)
				try {
					ps.close();
				} catch (Exception e) {
				}
			if (conn != null)
				try {
					conn.close();
				} catch (Exception e) {
				}
		}
		return data;
	}

	@Override
	public J23Member readOneMember(String m) {
		J23Member data = null;
		return data;
	}

}
